import java.util.concurrent.atomic.AtomicInteger;

public class demo5 {
    private static AtomicInteger totalSum = new AtomicInteger(0);//三个线程所加总和

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            int sum = 0;
            for (int i = 1; i <= 10; i++) {
                sum += i;
            }
            totalSum.addAndGet(sum); //t1线程所加和 更新到总和
        });

        Thread t2 = new Thread(() -> {
            int sum = 0;
            for (int i = 11; i <= 20; i++) {
                sum += i;
            }
            totalSum.addAndGet(sum); //t2线程所加和 更新到总和
        });

        Thread t3 = new Thread(() -> {
            int sum = 0;
            for (int i = 21; i <= 30; i++) {
                sum += i;
            }
            totalSum.addAndGet(sum); //t3线程所加和 更新到总和
        });

        t1.start();
        t2.start();
        t3.start();

        t1.join();
        t2.join();
        t3.join();

        System.out.println("3个线程所加总合" + totalSum.get());
    }
}
